import com.baidu.aip.ocr.AipOcr;
import org.json.JSONObject;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

/**
 * User: yian
 * Date: 2018/5/3
 */
public class IdentityCardDemo {//设置APPID/AK/SK
    public static final String APP_ID = "11187515";
    public static final String API_KEY = "bAr4nMnH9k3Zqb0pf9vXrqpY";
    public static final String SECRET_KEY = "202VlSyTEWDn54iO6UIxlda16q1e3GF3";

    public static void main(String[] args) throws IOException {
        // 初始化一个AipOcr
        AipOcr client = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        HashMap<String, String> options = new HashMap<String, String>();
        options.put("detect_direction", "true");
        options.put("detect_risk", "false");
        File file =new File("/Users/lww/IdeaProjects/code/myproject/baiduApi/src/main/resources/image/identitycard/result");
        FileWriter fileWriter = new FileWriter(file);
        for(int i=1;i<=10;i++) {
            // 参数为本地图片路径
            long start = System.currentTimeMillis();
//            System.out.println("picture"+i+"================================");
            String frontImage = "/Users/lww/IdeaProjects/code/myproject/baiduApi/src/main/resources/image/identitycard/"+i+".jpg";
            JSONObject frontRes = client.idcard(frontImage, "front", options);
//            System.out.println("front========================================");



//            System.out.println(frontRes.toString(2));



            // 参数为本地图片路径
            String backImage = "/Users/lww/IdeaProjects/code/myproject/baiduApi/src/main/resources/image/identitycard/"+i+"b.jpg";
            JSONObject backRes = client.idcard(backImage, "back", options);
//            System.out.println("back========================================");



//            System.out.println(backRes.toString(2));


            long end = System.currentTimeMillis();

            fileWriter.write("picture"+i+"================================");

            fileWriter.write("\n");
            fileWriter.write("耗时:"+(end-start)+"毫秒");
            fileWriter.write("\n");
            fileWriter.write("front========================================");
            fileWriter.write("\n");
            fileWriter.write(frontRes.toString(2));
            fileWriter.write("\n");
            fileWriter.write("back========================================");
            fileWriter.write("\n");
            fileWriter.write(backRes.toString(2));
            fileWriter.write("\n");

        }
        fileWriter.flush();
        fileWriter.close();






    }
}
